package com.apply.config;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author tianp
 */
public class EmailThreadFactory {
    /**
     * 发送邮件线程池
     *
     * @param name
     * @param size
     * @return
     */
    public static ThreadPoolExecutor getEmailThreadPool(final String name, int size) {
        return new ThreadPoolExecutor(size, size, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(size)
                , new ThreadFactory() {
            AtomicLong tomic = new AtomicLong(1);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, name + tomic.getAndAdd(1));

            }
        }, new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                try {
                    executor.getQueue().put(r);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
